import { http } from "../../utils/http";
import type { RootThunkAction } from "@/types/store";
import type {
  User,
  UserProfileResponse,
  UserProfile,
  UserPhotoResponse,
} from "@/types/data";
type UserResponse = {
  message: string;
  data: User;
};

// 我的页面 - 获取个人信息
export const getUser = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<UserResponse>("/user");
    const { data, message } = res.data;
    dispatch({ type: "profile/getUser", payload: data });
  };
};
export const getUserProfile = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<UserProfileResponse>("/user/profile");
    dispatch({ type: "profile/getUserProfile", payload: res.data.data });
  };
};
export const updateUserProfile = (
  userProfile: Partial<UserProfile>
): RootThunkAction => {
  return async (dispatch) => {
    await http.patch("/user/profile", userProfile);
    // 分发action更新用户昵称
    dispatch({ type: "profile/update", payload: userProfile });
  };
};
export const updateUserPhoto = (data: FormData): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.patch<UserPhotoResponse>("/user/photo", data);

    dispatch({
      type: "profile/update",
      payload: {
        photo: res.data.data.photo,
      },
    });
  };
};
